/*
 *  Copyright 2016 http://www.kedacomframework.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.kedacom.ctsp.authz.oauth2.service;

import com.kedacom.ctsp.authz.oauth2.core.ErrorType;
import com.kedacom.ctsp.authz.oauth2.core.OAuth2Exception;
import com.kedacom.ctsp.authz.oauth2.entity.Client;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * TODO 完成注释
 *
 * @author
 */
public abstract class AbstractAuthorizationService {

    @Autowired
    protected AccessTokenService accessTokenService;
    @Autowired
    protected ClientService clientService;

    protected void assertGrantTypeSupport(Client client, String grantType) {
        if (!client.isSupportGrantType(grantType)) {
            throw new OAuth2Exception(ErrorType.UNSUPPORTED_GRANT_TYPE);
        }
    }

    protected void assertParameterNotBlank(String parameter, ErrorType type) {
        if (null == parameter || parameter.isEmpty()) {
            throw new OAuth2Exception(type);
        }
    }

    protected Client getClient(String clientId, String clientSecret) {
        Client client = getClient(clientId);
        clientService.validateClient(clientId, clientSecret);
        return client;
    }


    protected Client getClientByOwnerId(Long ownerId ,String clientId) {
        return clientService.getCheckedClientByOwnerId(ownerId,clientId);
    }

    protected Client getClient(String clientId) {
        return clientService.getCheckedClient(clientId);
    }

}
